//
//  ForgetPasswordVerifyViewController.m
//  HuLaQuan
//
//  Created by hok on 1/19/16.
//  Copyright © 2016 Jianghao. All rights reserved.
//

#import "ForgetPasswordVerifyViewController.h"

#import "YuTypeInfoCell.h"
#import "ConfirmButtonCell.h"

//labels
#import "YuUserCellLabel.h"

//Jump To Controller
#import "ForgetPasswordViewController.h"

//Utility
#import "YuLoginRegisterUtilities.h"

@interface ForgetPasswordVerifyViewController ()<UITextFieldDelegate,UIGestureRecognizerDelegate>

@property (nonatomic,strong) NSArray *items;

@property (nonatomic,strong) NSString *mobile;

@end


static NSString * const YuTypeInfoCellIdentifier = @"YuTypeInfoCell";
static NSString * const ConfirmButtonCellIdentifier = @"ConfirmButtonCell";

@implementation ForgetPasswordVerifyViewController

- (NSArray *)items
{
    if (!_items) {
        _items = @[@"用户名"];
    }
    return _items;
}

- (void)setupUI
{
    [self setupNavBackButton];
    
    [self setupFooterForTableView];
    
    [self.tableView registerNib:[YuTypeInfoCell nib] forCellReuseIdentifier:YuTypeInfoCellIdentifier];
    [self.tableView registerNib:[ConfirmButtonCell nib] forCellReuseIdentifier:ConfirmButtonCellIdentifier];
    
    self.title = @"忘记密码";
    
    [self setupTapGesture];
}

#pragma mark UITableViewDataSource

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    return indexPath.row ==0 ? kNormalCellHeight : kConfirmButtonCellHeight;
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 2;
    
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    
    return 40;
    
    
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section

{
    

    CGRect frameRect = CGRectMake(50, 0, 200, 40);
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 200, 40)];
    YuUserCellLabel *textSection = [[YuUserCellLabel alloc] initWithFrame:frameRect];
    textSection.text = @"基本信息";
    [view addSubview:textSection];
    return view;
    
    
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    if (indexPath.row == 0) {
        YuTypeInfoCell *cell = [tableView dequeueReusableCellWithIdentifier:YuTypeInfoCellIdentifier
                                                                 forIndexPath:indexPath];
        cell.textField.placeholder = kNumberFiledPlaceholder;
        
        cell.textField.delegate = self;
        cell.textField.tag = indexPath.row;
        cell.textField.keyboardType = UIKeyboardTypePhonePad;
        [cell.textField addTarget:self action:@selector(detaileAddressFieldWithText:) forControlEvents:UIControlEventEditingChanged];
        return cell;
        
    }else{
        ConfirmButtonCell *cell = [tableView dequeueReusableCellWithIdentifier:ConfirmButtonCellIdentifier
                                                                 forIndexPath:indexPath];
        
        [cell.confirmButton setTitle:@"下一步" forState:UIControlStateNormal];
        
        cell.confirmButton.enabled = self.mobile.length ? YES : NO;
        
        cell.confirm = (ClickBlock)^(){
            [self tapNextButton];
        };
        
        return cell;
    }
    
    
    
    
}
//getInformation
- (void)detaileAddressFieldWithText:(UITextField *)textField
{

    self.mobile = textField.text;

}


- (void)tapNextButton
{
    if (self.mobile && [YuLoginRegisterUtilities isMobileNumber:self.mobile] ) {
        [self performSegueWithIdentifier:@"ShowForgetPasswordViewController" sender:self];
    }else{
        [self.view makeToast:kMobileNumberInavalid];
    }
    
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
}


#pragma mark - Textfield delegate
- (void)textFieldDidBeginEditing:(UITextField *)textField{
    
    textField.placeholder = @"";
    
}
- (void)textFieldDidEndEditing:(UITextField *)textField{
    if (textField.text.length == 0 ) {
        textField.placeholder = kNumberFiledPlaceholder;
        
    }
    if (self.mobile.length == 0) {
        NSIndexPath *indexPath = [NSIndexPath indexPathForRow:1 inSection:0];
        ConfirmButtonCell *cell = [self.tableView cellForRowAtIndexPath:indexPath];
        cell.confirmButton.enabled = NO;
    }
}
-(BOOL)textFieldShouldReturn:(UITextField *)textField{
    [textField resignFirstResponder];
    return YES;
}
- (BOOL)textField:(UITextField*)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
    
    if (self.mobile.length > 0) {
        
        
        NSIndexPath *indexPath = [NSIndexPath indexPathForRow:1 inSection:0];
        ConfirmButtonCell *cell = [self.tableView cellForRowAtIndexPath:indexPath];
        cell.confirmButton.enabled = YES;
    }
    return YES;
    
    // return [self validateNumber:string];
}


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


#pragma mark - Navigation


- (void)goBackAction
{
    [[UIApplication sharedApplication].keyWindow endEditing:YES];
    UIAlertController *x = [UIAlertController alertWithTitle:@"提示" andMessage:@"是否放弃重置登录密码" andBlock:^(UIAlertAction *action ) {
        if ([action.title isEqualToString:@"好的"]) {
            [self.navigationController popToRootViewControllerAnimated:YES];
        
        }
        
    }  type:@"confirmAndCancel"];
    [self presentViewController:x animated:YES completion:nil];
}

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
    if ([segue.identifier isEqualToString:@"ShowForgetPasswordViewController"]) {
        ForgetPasswordViewController *con = (ForgetPasswordViewController *)[segue destinationViewController];
        con.userInfo[@"userName"] = self.mobile;
    }
    
}


@end
